package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//管理端套餐的控制器
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //分页查询列表
    @GetMapping("/page")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 String name) {
        //调用service
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);
        //返回
        return ResultInfo.success(page);
    }

    //套餐新增
    @PostMapping
    public ResultInfo save(@RequestBody Setmeal setmeal) {
        //调用service
        setmealService.save(setmeal);
        //返回成功
        return ResultInfo.success();
    }

    //修改套餐之回显套餐
    @GetMapping("/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        //调用service
        Setmeal setmeal = setmealService.findById(id);
        //返回
        return ResultInfo.success(setmeal);
    }

    //修改套餐
    @PutMapping
    public ResultInfo update(@RequestBody Setmeal setmeal) {
        //调用service
        setmealService.update(setmeal);
        //返回
        return ResultInfo.success();
    }

    //批量启停售
    @PostMapping("/status/{status}")
    public ResultInfo stopBuy(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //调用service
        setmealService.stopBuy(status, ids);
        //返回
        return ResultInfo.success();
    }

    //批量删除
    @DeleteMapping
    public ResultInfo delete(@RequestParam List<Long> ids) {
        //调用service
        String deleteName = setmealService.delete(ids);
        //判断是否有在售套餐
        if (deleteName != "") {
            return ResultInfo.error(deleteName.substring(0,deleteName.length() -1) + "在售,不能删除!");
        }
        //返回
        return ResultInfo.success();
    }


    //@GetMapping("/{id}")
    //public ResultInfo findByIdp(@PathVariable Long id) {
    //    Setmeal byId = setmealService.findById(id);
//
    //    return ResultInfo.success(byId);
    //}
}